import { getMessages } from '../api/message'
import { INIT_MESSAGES, MARK_READED_BY_ID, MARK_READED_ALL } from './action-types'

export const getMessagesAction = () => dispatch => {
  getMessages().then(res => {
    console.log(res)
    dispatch({
      type: INIT_MESSAGES,
      payload: res.list
    })
  }).catch(e => console.log('获取消息：', e))
}

export const markReadedByIdAction = id => dispatch => {
  // 模拟异步请求服务器操作
  setTimeout(() => {
    dispatch({
      type: MARK_READED_BY_ID,
      payload: {
        id
      }
    })
  }, 1000)
}

export const markReadedAllAction = () => dispatch => {
  // 模拟异步请求服务器操作
  setTimeout(() => {
    dispatch({
      type: MARK_READED_ALL
    })
  }, 1000)
}
